package com.micro.ai.template.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.micro.ai.template.entity.TemplateStatistic;
import com.micro.ai.template.mapper.TemplateStatisticMapper;
import com.micro.ai.template.service.TemplateStatisticService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * 模板统计Service实现
 *
 * @author micro-ai
 * @since 0.0.1
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class TemplateStatisticServiceImpl extends ServiceImpl<TemplateStatisticMapper, TemplateStatistic>
        implements TemplateStatisticService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public TemplateStatistic getOrCreateStatistic(String templateId) {
        LambdaQueryWrapper<TemplateStatistic> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TemplateStatistic::getTemplateId, templateId);
        
        TemplateStatistic statistic = this.getOne(wrapper);
        if (statistic == null) {
            statistic = new TemplateStatistic();
            statistic.setTemplateId(templateId);
            statistic.setStatDate(java.time.LocalDate.now());
            statistic.setViewCount(0);
            statistic.setDownloadCount(0);
            statistic.setUseCount(0);
            statistic.setFavoriteCount(0);
            statistic.setRatingCount(0);
            statistic.setAvgRating(java.math.BigDecimal.ZERO);
            statistic.setCreatedAt(LocalDateTime.now());
            this.save(statistic);
        }
        
        return statistic;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void incrementUsageCount(String templateId) {
        TemplateStatistic statistic = getOrCreateStatistic(templateId);
        statistic.setUseCount(statistic.getUseCount() + 1);
        this.updateById(statistic);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void incrementSuccessCount(String templateId) {
        // 使用use_count表示成功使用次数
        incrementUsageCount(templateId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void incrementFailureCount(String templateId) {
        // 目前表中没有失败计数字段，此方法暂时不做操作
        log.warn("incrementFailureCount not implemented - no failure_count field in table");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateAverageRating(String templateId, double rating) {
        TemplateStatistic statistic = getOrCreateStatistic(templateId);
        statistic.setAvgRating(java.math.BigDecimal.valueOf(rating));
        statistic.setRatingCount(statistic.getRatingCount() + 1);
        this.updateById(statistic);
    }

    @Override
    public Map<String, Object> getStatistics(String templateId) {
        TemplateStatistic statistic = getOrCreateStatistic(templateId);
        
        Map<String, Object> result = new HashMap<>();
        result.put("viewCount", statistic.getViewCount());
        result.put("downloadCount", statistic.getDownloadCount());
        result.put("useCount", statistic.getUseCount());
        result.put("favoriteCount", statistic.getFavoriteCount());
        result.put("ratingCount", statistic.getRatingCount());
        result.put("avgRating", statistic.getAvgRating());
        
        return result;
    }
}

